<?php
namespace app\api\business;
use app\common\model\redis\RedisModel;
use app\common\model\mysql\Label as LabelModel;
class LabelBus
{
    /**
     * 基本列表获取
     * @param array $data
     * @return array
     * @throws Exception
     */
    public function listData(array $data,array $additional=[])
    {
        try {
            if(empty($data) || !isset($data['page']) || !isset($data['limit'])){
                return false;
            }
            // 限定条件
            $where = array();
            if(!empty($data['id'])) {
                $where[]=['id', '=', $data['id']];
            }
            if(!empty($data['type'])) {
                $where[]=['type', '=', $data['type']];
            }
            if(!empty($data['expand'])) {
                $where[]=['expand', 'like', "%{$data['expand']}%"];
            }
            $whereOr = array();
            if($additional){
                $where = array_merge($where,$additional);
            }
            $where[]=['status', '<>', 2];
            // 99标记删除记录
            // 需要显示的字段，默认空全部显示
            $displayField=[];
            // 需要排除显示的字段，默认空全部显示,优先级大
            $hideField = [];
            // 排序
            $order = ["id"=>"desc"];
            // 分页
            $page = $data['page'];
            $limit =  $data['limit'];
            if($page==0 || empty($limit)){
                $page = 1;
            }
            if($data['limit'] ==0 || empty($data['limit'])){
                $limit = 10;
            }
            $obj = new LabelModel();
            // 查询数据
            $res = $obj->getListData($where,$displayField,$hideField,$order,$page,$limit,$whereOr);
            if(empty($res)){
                return array();
            }
            return $res->toArray();
        }catch (\Exception $e){
            exception($e,'LabelBus.listData');
        }
    }

    /**
     * 不带分页
     * @param array $data
     * @return array
     * @throws Exception
     */
    public function noPagingList($data = [])
    {
        try {
            // 限定条件
            $where = array();
            if(!empty($data['id'])) {
                $where[]=['id', '=', $data['id']];
            }
            if(!empty($data['type'])) {
                $where[]=['type', '=', $data['type']];
            }
            if(!empty($data['name'])) {
                $where[]=['name', 'like', "%{$data['name']}%"];
            }
            // 99标记删除记录
            // 需要显示的字段，默认空全部显示
            $displayField=[];
            // 需要排除显示的字段，默认空全部显示,优先级大
            $hideField = [];
            // 排序
            $order = ["id"=>"desc"];
            // 分页
            $page = -1;
            $limit = -1;
            // 查询数据
            $obj = new LabelModel();
            $res = $obj->getListData($where,$displayField,$hideField,$order,$page,$limit);
            if(empty($res)){
                return array();
            }
            return $res->toArray();
        }catch (\Exception $e){
            exception($e,'LabelBus.noPagingList');
        }
    }

    /**
     * 根据单个字段名称获取单个信息
     * @param string $fieldName // 字段名称
     * @param string $fieldValue // 字段值
     * @param bool $condition   // 条件
     * @return false
     * @throws Exception
     */
    public function getFieldInfo(string $field='', string $val='', bool $condition=true){
        try {
            if(empty($field) || empty($val)){
                return false;
            }
            // 限定条件
            $where = array();
            $whereOr = array();
            // 查询判断
            if($condition){
                $condition = '=';
            }else{
                $condition = '<>';
            }
            $where[]=[$field, $condition, $val];
            // 需要显示的字段，默认空全部显示
            $displayField=[];
            // 需要排除显示的字段，默认空全部显示,优先级大
            $hideField = [];
            // 排序
            $order = ["id"=>"desc"];
            // 查询信息
            $obj = new LabelModel();
            $res =  $obj->getFindModel($where,$whereOr,$displayField,$hideField,$order);
            if ($res){
                return $res->toArray();
            }
            return array();
        }catch (\Exception $e){
            exception($e,'LabelBus.getFieldInfo');
        }
    }

    /**
     * 查询信息
     * @param string $field
     * @param string $val
     * @param bool $condition
     * @return array|false|void
     */
    public function getInfo(string $field='', string $val='', bool $condition=true){
        try {
            if(empty($field) || empty($val)){
                return false;
            }
            // 限定条件
            $where = array();
            $whereOr = array();
            // 查询判断
            if($condition){
                $condition = '=';
            }else{
                $condition = '<>';
            }
            $where[]=[$field, $condition, $val];
            // 需要显示的字段，默认空全部显示
            $displayField=[];
            // 需要排除显示的字段，默认空全部显示,优先级大
            $hideField = [];
            // 排序
            $order = ["id"=>"desc"];
            // 查询信息
            $obj = new LabelModel();
            $res =  $obj->getInfo($where,$whereOr,$displayField,$hideField,$order);
            if ($res){
                return $res->toArray();
            }
            return array();
        }catch (\Exception $e){
            exception($e,'LabelBus.getInfo');
        }
    }
    /**
     * 获取多个主键ID信息
     * @param $where // 主键ID数组
     * @return mixed
     * @throws Exception
     */
    public function multiplePrimaryKeys($IdArray){
        try {
            $obj = new LabelModel();
            $res = $obj->multiplePrimaryKeys($IdArray);
            if($res){
                return $res->toArray();
            }
            return array();
        }catch (\Exception $e){
            exception($e,'CommonBus.multiplePrimaryKeys');
        }
    }

    /**
     * 批量操作多条数据/新增
     * @param $list // 新增的数据
     * @throws Exception
     */
    public function batchUpdate($list)
    {
        try {
            $obj = new LabelModel();
            return $obj->saveAll($list);
        }catch (\Exception $e){
            exception($e,'CommonBus.batchUpdate');
        }
    }

    /**
     * 添加
     * @param $data // 添加的数据
     * @param $data
     * @throws Exception
     */
    public function addData($data){
        try {
            $obj = new LabelModel();
            return $obj->save($data);
        }catch (\Exception $e){
            exception($e,'CommonBus.addData');
        }
    }

    /**
     * 更新信息
     * @param $data
     * @throws Exception
     */
    public function updateData($data){
        try {
            $obj = new LabelModel();
            return $obj->updateDataModel($data)->toArray();
        }catch (\Exception $e){
            exception($e,'CommonBus.updateData');
        }
    }

    /**
     * 获取数据极大值和极小值
     * @param string $field
     * @param bool $condition
     * @return array|false|void
     * @throws Exception
     */
    public function getExtremum(string $field='', bool $condition=true){
        try {
            if(empty($field) || empty($condition)){
                return false;
            }
            if($condition){
                $condition = "desc";
            }else{
                $condition = "asc";
            }
            // 限定条件
            $where = array();
            $whereOr = array();
            // 需要显示的字段，默认空全部显示
            $displayField=[];
            // 需要排除显示的字段，默认空全部显示,优先级大
            $hideField = [];
            // 排序
            $order = [$field=>$condition];
            // 查询信息
            $obj = new LabelModel();
            $res =  $obj->getFindModel($where,$whereOr,$displayField,$hideField,$order);
            if ($res){
                return $res->toArray();
            }
            return array();
        }catch (\Exception $e){
            exception($e,'CommonBus.getExtremum');
        }
    }

    /**
     * 根据主键删除信息
     * @param $id // 主键ID
     * @throws Exception
     */
    public function deleteData($id){
        try {
            $obj = new LabelModel();
            return $obj->destroy($id);
        }catch (\Exception $e){
            exception($e,'CommonBus.deleteData');
        }
    }

    /**
     * 根据非主键条件删除信息
     * @param $ID
     * @return array|false|\josn|void
     * @throws Exception
     */
    public function conditionDeletion($field='',$val=''){
        try {
            if(empty($field) || empty($val)){
                return false;
            }
            // 删除信息
            $obj = new LabelModel();
            return $obj->destroy([$field=>$val]);
        }catch (\Exception $e){
            exception($e,'CommonBus.conditionDeletion');
        }
    }
}